<template>
    <view style="height: 100%">
      <scroll-view :scroll-y="true" :scroll-x="false" class="scroll" @scrolltolower="bindlower">
        <view class="swiper">
            <block v-for="(item, index) in marketList" :key="index">
                <!-- parse <template is="mymarket" :data="item"/> -->
                <block name="mymarket">
                    <view class="item">
                        <view class="ddhead">
                            <text class="ddhao">{{ item.productName }}：{{ item.productDesc }}</text>
                        </view>
                        <block v-for="(item, index) in item.productPics" :key="index">
                            <view class="ddimg">
                                <image :src="item" />
                            </view>
                        </block>
                        <view class="ddfooter">
                            <text class="jr">销售：￥{{ item.amount }}</text>
                            <text class="state">
                                <block>分销：￥{{ item.marketAmount }}</block>
                            </text>
                        </view>
                    </view>
                </block>
            </block>
            <view class="prompt" v-if="marketList.length == 0">
                <image src="/static/images/dd.png" />
                <text>您没有任何分销</text>
            </view>
        </view>
        <!-- template对应的原始代码，为保证正常显示，已对其进行隐藏。 -->
        <block name="mymarket" v-if="false">
            <view class="item">
                <view class="ddhead">
                    <text class="ddhao">{{ item.productName }}：{{ item.productDesc }}</text>
                </view>
                <block v-for="(item, index) in item.productPics" :key="index">
                    <view class="ddimg">
                        <image :src="item" />
                    </view>
                </block>
                <view class="ddfooter">
                    <text class="jr">销售：￥{{ item.amount }}</text>
                    <text class="state">
                        <block>分销：￥{{ item.marketAmount }}</block>
                    </text>
                </view>
            </view>
        </block>
        <view style="padding-bottom: 30px"></view>
        <view class="footer">
            <view class="left">
                <text class="p">分销金合计:</text>
                <text class="span">¥{{ sumAmount }}</text>
            </view>
        </view>
      </scroll-view>  
    </view>
</template>

<script>
var config = require('../../config.js');
var http = require('../../utils/httpHelper.js');
export default {
    data() {
        return {
            userInfo: {},
            marketList: [],
            timer: {},
            sumAmount: 0,
            marketPage: {
                current: 1,
                pageSize: 10,
                total: 0
            }
        };
    },
    onLoad: function () {
        var that = this;
        let userInfo = uni.getStorageSync('user');
        that.setData({
            userInfo: userInfo
        });
    },
    onShow: function () {
        var that = this;
        this.getlist(function (list) {
            var sumAmount = 0;
            list.forEach((item) => {
                sumAmount = sumAmount + item.marketAmount;
            });
            that.setData({
                marketList: list,
                sumAmount: sumAmount.toFixed(2)
            });
        });
    },
    methods: {
        getMoreData() {
            var that = this;
            that.marketPage.current++;
            that.getlist(function (list) {
                let yList = that.marketList;
                yList = yList.concat(list);
                var sumAmount = 0;
                yList.forEach((item) => {
                    sumAmount = sumAmount + item.marketAmount;
                });
                that.setData({
                    marketList: yList,
                    sumAmount: sumAmount.toFixed(2)
                });
            });
        },

        getlist: function (callback) {
            var that = this;
            http.httpPost(
                config.clubApi.getMarketList,
                {
                    pageNo: that.marketPage.current,
                    pageSize: that.marketPage.pageSize,
                    createBy: that.userInfo.userId,
                    productType:-2
                },
                function (res) {
                    if (res.code == '200') {
                        var list = res.data.pageData;
                        if (typeof callback == 'function') {
                            callback(list);
                        }
                    }
                }
            );
        },

        //处理分页
        bindlower: function (e) {
          if(this.timer != null){
            clearTimeout(this.timer)
          }
          this.timer = setTimeout(()=>{
            this.getMoreData();
          },500)
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
@import '@/pages/market/index.scss';
</style>
